<?php

namespace ai\app\web;


class Auth
{
    public function verify($permissionID)
    {

        $sql = "SELECT b.id,b.req_method from sys_auth_modules_actions_permissions a
 INNER JOIN sys_auth_modules_actions_permissions b on a.module_id=b.module_id and a.action_id=b.action_id
 INNER JOIN sys_auth_modules c on a.module_id=c.id
 INNER JOIN sys_auth_modules_actions d on a.action_id=d.id
 where a.id={$permissionID} and c.`status`=1 and c.state=0 and d.`status`=1 and d.state=0 and a.`status`=1 and a.state=0";

        $rows = \Ai::$db->query->sql($sql)->all();

        $perIDs = $permissionID;
        $curPerMethod = "";
        foreach ($rows as $item) {
            if ($item['id'] == $permissionID)
                $curPerMethod = $item['req_method'];
        }

        if (!empty($curPerMethod)) {
            foreach ($rows as $r)
            {
                if($curPerMethod=='get'&&$r['req_method']=='getall')
                    $perIDs.=",".$r['id'];
                if($curPerMethod=='post'&&$r['req_method']=='postall')
                    $perIDs.=",".$r['id'];
            }

            $user=\Ai::$app->web->user->getIdentity();
            $role_id=isset($user['role_id'])?$user['role_id']:0;


            $sql="SELECT a.* from sys_auth_role_permissions a
INNER JOIN sys_auth_role b on a.role_id=b.id 
where a.role_id in ({$role_id}) and a.permissions_id in ({$perIDs}) and b.`status`=1";

            $hasPer = \Ai::$db->query->sql($sql)->all();

            if(count($hasPer)>0)
                return true;
        }
        return false;
    }
}

